package com.wilcom.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wilcom.system.common.BaseResponse;
import com.wilcom.system.common.ErrorCode;
import com.wilcom.system.common.PageRequest;
import com.wilcom.system.common.ResultUtils;
import com.wilcom.system.domain.Repairrequest;
import com.wilcom.system.domain.Repairtask;
import com.wilcom.system.exception.ThrowUtils;
import com.wilcom.system.model.request.repairrequest.AssignTechnicianRequest;
import com.wilcom.system.model.request.repairrequest.ConfirmRepairRequest;
import com.wilcom.system.model.request.repairrequest.SetUrgencyLevelRequest;
import com.wilcom.system.model.request.repairtask.GetRepairRequestsBy;
import com.wilcom.system.service.RepairrequestService;
import com.wilcom.system.service.RepairtaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
      
import javax.annotation.Resource;

@RestController
@RequestMapping("/repairrequest")
@Slf4j
public class RepairRequestController {
    @Resource
    RepairrequestService repairrequestService;

    @Resource
    RepairtaskService repairtaskService;
//- getAllRepairRequests() - 查看所有报修请求。
    @PostMapping("/getAllRepairRequests")
    public BaseResponse<Page<Repairrequest>> getAllRepairRequests(@RequestBody PageRequest postQueryRequest) {
        long current = postQueryRequest.getCurrent();
        long size = postQueryRequest.getPageSize();
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Repairrequest> repairrequestPage = repairrequestService.page(new Page<>(current, size),
                repairrequestService.getQueryWrapper(postQueryRequest));
        return ResultUtils.success(repairrequestPage);
    }
    @PostMapping("/getRepairRequestsBy")
    public BaseResponse<Page<Repairrequest>> getRepairRequestsBy(@RequestBody GetRepairRequestsBy postQueryRequest) {
        long current = postQueryRequest.getCurrent();
        long size = postQueryRequest.getPageSize();
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Repairrequest> repairrequestPage = repairrequestService.page(new Page<>(current, size),
                repairrequestService.GetRepairRequestsBy(postQueryRequest));
        return ResultUtils.success(repairrequestPage);
    }
    @PostMapping("/getAllPendingRepairRequests")
    public BaseResponse<Page<Repairrequest>> getAllPendingRepairRequests(@RequestBody PageRequest postQueryRequest) {
        long current = postQueryRequest.getCurrent();
        long size = postQueryRequest.getPageSize();
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Repairrequest> repairrequestPage = repairrequestService.page(new Page<>(current, size),
                repairrequestService.getPendingQueryWrapper(postQueryRequest));
        return ResultUtils.success(repairrequestPage);
    }
//            - confirmRepairRequest(requestId, status) - 确认报修请求（通过或驳回）。
    @PostMapping("/confirmRepairRequest")
    public BaseResponse<Boolean> confirmRepairRequest(@RequestBody ConfirmRepairRequest request) {
        Repairrequest repairrequest = new Repairrequest();
        repairrequest.setStatus(request.getStatus());
        repairrequest.setRequestId(request.getRequestId());
        return ResultUtils.success(repairrequestService.updateById(repairrequest));
    }
//            - setUrgencyLevel(requestId, level) - 填写报修请求紧急程度。
    @PostMapping("/setUrgencyLevel")
    public BaseResponse<Boolean> setUrgencyLevel(@RequestBody SetUrgencyLevelRequest request) {
        Repairrequest repairrequest = new Repairrequest();
        repairrequest.setUrgencyLevel(request.getUrgencyLevel());
        repairrequest.setRequestId(request.getRequestId());
        return ResultUtils.success(repairrequestService.updateById(repairrequest));
    }
//            - assignTechnician(requestId, technicianId) - 分配维修人员。
    @PostMapping("/assignTechnician")
    public BaseResponse<Boolean> assignTechnician(@RequestBody AssignTechnicianRequest request) {
        Repairtask repairtask = new Repairtask();
        repairtask.setRequestId(request.getRequestId());
        repairtask.setAssignedTechnicianId(request.getTechnicianId());
        return ResultUtils.success(repairtaskService.updateById(repairtask));
    }
//            - getRepairRequestStatus(requestId) - 查看特定报修任务的状态。

    @PostMapping("/getRepairRequestStatus")
    public BaseResponse<Object> getRepairRequestStatus(@RequestBody Integer requestId) {
        Repairrequest repairrequest = repairrequestService.getById(requestId);
        return ResultUtils.success(repairrequest.getStatus());
    }

}
